#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <CoreGraphics/CGColor.h>
#import "SSSSH.h"
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include <notify.h>
#define APP_NAME "SSH.app"
#define SERVICE_NAME "SSH"
#define SERVICE_COM "com.openssh.sshd"
#define SERVICE_PLIST "com.openssh.sshd.plist"

@implementation SSSSH

- (BOOL)serviceIsON
{
    NSArray *ctls = [self getctl];

    BOOL match = NO;
    int j;
    for (j = 0; j < [ctls count]; j++)if ([@SERVICE_COM isEqualToString:[ctls objectAtIndex:j]])match = YES;
	if(match){
   return 	YES;
   } else {
   return 	NO;
   }
}

- (void)dismissAlert:(NSTimer *)why
{
	NSString *str = @"Applications/SpringSwitches.app/Switches.plist";
	FILE *fp;
	if(fp = fopen([str UTF8String], "r")){
		notify_post("com.apple.language.changed");
	} else {
		[self terminateWithSuccess];
	}
}

- (void)runCommand: (int) commandTxt {
	whichAction2 = [NSString stringWithFormat: commandTxt];
	char *cmd2 = [whichAction2 UTF8String];
	FILE *fp2 = popen(cmd2, "r");
	if (fp2 == NULL) {
		NSLog(@"popen failed"); 
		exit(1);
	}

   NSString *riz2 = @"";
   int c2;
   int kBlockSize = 512;

   do {
       char buf2[kBlockSize];
       c2 = fread(buf2, sizeof(char), kBlockSize, fp2);
       if (c2 <= 0) break;
       buf2[c2] = '\0';   
       riz2 = [riz2 stringByAppendingString:
           [NSString stringWithCString:buf2 encoding:1]];
   } while (1);
}
- (NSMutableArray *) getctl
{
   char *cmd = [@"/bin/launchctl list" UTF8String];
   FILE *fp = popen(cmd, "r");
   if (fp == NULL) {NSLog(@"popen failed"); exit(1);}

   NSString *riz = @"";
   int c;
   int kBlockSize = 512;

   do {
       char buf[kBlockSize];
       c = fread(buf, sizeof(char), kBlockSize, fp);
       if (c <= 0) break;
       buf[c] = '\0';   
       riz = [riz stringByAppendingString:
           [NSString stringWithCString:buf encoding:1]];
   } while (1);

   int i;
   id st;

   NSArray *xarray = [riz componentsSeparatedByString:@"\n"];
   NSMutableArray *outarray = [[NSMutableArray alloc] init];

   for (i = 2; i < [xarray count]; i++)
   {
      st = [[xarray objectAtIndex:i] componentsSeparatedByString:@"	"];
      if ([st count] == 4)
        [outarray addObject:[st objectAtIndex:3]];
   }

   return outarray;   
}
- (void)applicationDidFinishLaunching:(id)unused
{
    UIAlertSheet	*sheet = [[UIAlertSheet alloc]
		initWithFrame:[UIHardware fullScreenApplicationContentRect]];

    if([self serviceIsON]) {
		NSString *str = @"Applications/SpringSwitches.app/Switches.plist"; /*Check if icon change is needed*/
		FILE *fp;
		if(fp = fopen([str UTF8String], "r")){
			whichAction = [NSString stringWithFormat:@"/bin/cp Applications/%@/on.png Applications/%@/icon.png",
			@APP_NAME, @APP_NAME];
			[self runCommand: whichAction];
		}
		[sheet setTitle: [NSString stringWithFormat:@"Turning %@ OFF...", @SERVICE_NAME]];
		[self runCommand:[NSString stringWithFormat:@"/bin/launchctl unload -w /Library/LaunchDaemons/%@", @SERVICE_PLIST]];
    } else {
		NSString *str = @"Applications/SpringSwitches.app/Switches.plist"; /*Check if icon change is needed*/
		FILE *fp;
		if(fp = fopen([str UTF8String], "r")){
			whichAction = [NSString stringWithFormat:@"/bin/cp Applications/%@/off.png Applications/%@/icon.png",
			@APP_NAME, @APP_NAME];
			[self runCommand: whichAction];
		}
		[sheet setTitle: [NSString stringWithFormat:@"%@  still OFF.", @SERVICE_NAME]];
		[sheet setBodyText: @"Use Services or BossPrefs to turn it back on... Sorry!"];
		[self runCommand:[NSString stringWithFormat:@"/bin/launchctl load -wF /Library/LaunchDaemons/com.openssh.sshd.plist.", @SERVICE_PLIST ]];
    }

    [sheet setDelegate:self];
    [sheet setRunsModal:NO];
    [sheet popupAlertAnimated:NO];
	
    NSTimer	*timer = [NSTimer scheduledTimerWithTimeInterval:3.0
			    target:self selector:@selector(dismissAlert: )
			    userInfo:nil repeats:NO];
}

@end

